#include <iostream>
#include <stdlib.h>
#include <functional>
#include <thread>
#include <string>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <memory>
#include <assert.h>
#include <algorithm>
#include <queue>
#include <unistd.h>


#ifndef THREADPOOL
#define THREADPOOL

class ThreadPool{
public:
	typedef std::function<void()> Task;

	ThreadPool(int num);
	~ThreadPool();

	ThreadPool(const ThreadPool&) = delete;
	void operator=(const ThreadPool&) = delete;

	void setMaxQueueSize(int maxSize);
	void start();
	void stop();
	void run(const Task &t);

private:
	bool isFull() const;
	void threadFunc();
	Task take();

private:
	int num_;
	std::mutex mutex_;
	std::condition_variable notEmpty_;              //条件变量
	std::condition_variable notFull_;
	std::vector<std::thread> threads_;
	std::deque<Task> queue_;
	size_t maxQueueSize_;
	bool running_;
};

#endif

/*void fun(int num)
{
	std::cout << "[id:" << std::this_thread::get_id() << "] hello, world!" << num <<std::endl;
	sleep(1);
}

int main(int argc, char* argv[])
{
	{
		std::cout << "main thread id:" << std::this_thread::get_id() << std::endl;
		ThreadPool pool(3);
		pool.setMaxQueueSize(100);
		pool.start();
		//std::this_thread::sleep_for(std::chrono::milliseconds(3000));

		for (int i = 0; i < 10; i++) {
            auto fun_c = std::bind(fun, i);
			pool.run(fun);
		}
		std::this_thread::sleep_for(std::chrono::milliseconds(3000));
	}

	return 0;
}*/

